﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.Utils;

namespace QldtSdh.GUI.XetTotNghiep
{
    public partial class FormInBangDiemFinal : BaseForm
    {
        private string filePathTemp = Application.StartupPath + "\\{0}.doc";
        private int timeToWaitExport = 3000;
        WordUtil word = null;
        public FormInBangDiemFinal()
        {
            InitializeComponent();
            var rs = ConfigUtil.GetConfig("timetowaitexport");
            if (rs != null && rs.Count == 1)
                try
                {
                    timeToWaitExport = int.Parse(rs[0].Value);
                }
                catch
                {
                    timeToWaitExport = 3000;
                }
        }

        private void FormInBangDiemFinal_Load(object sender, EventArgs e)
        {
            this.rvInBangDiemFinal.RefreshReport();
        }
        public void ShowOrPrint(HocVien hocVien, List<ChuongTrinhDaoTao_HocPhan> ctdt, NganhDaoTao chuyenNganh, int khoaNam, bool preview = true, bool printMultifile = false)
        {
            List<ChuongTrinhDaoTao_HocPhan> ctdt1 = new List<ChuongTrinhDaoTao_HocPhan>();
            List<ChuongTrinhDaoTao_HocPhan_Clone> ctdt2 = new List<ChuongTrinhDaoTao_HocPhan_Clone>();

            /*For tesst*/
            //if (ctdt.Count < 26)
            //{
            //    int total = ctdt.Count;
            //    for (int i = 0; i < (26 - total); i++)
            //    {
            //        ctdt.Add(new ChuongTrinhDaoTao_HocPhan { TenHocPhan = "Test" });
            //    }
            //}
            /*End test*/
            int divBangDiem = (int)Math.Ceiling(ctdt.Count / 2.0);
            for (int i = 0; i < ctdt.Count; i++)
            {
                var item = ctdt[i];
                if (i >= divBangDiem)
                {
                    ctdt2.Add(new ChuongTrinhDaoTao_HocPhan_Clone
                    {
                        Diem = item.Diem,
                        MaHocPhan_Real = item.MaHocPhan_Real,
                        SoTinChi = item.SoTinChi,
                        TenHocPhan = item.TenHocPhan
                    });
                }
                else
                {
                    ctdt1.Add(item);
                }
            }
            ChuongTrinhDaoTao_HocPhanBindingSource.DataSource = ctdt1;
            ChuongTrinhDaoTao_HocPhan_CloneBindingSource.DataSource = ctdt2;
            ReportParameter[] parameters = new ReportParameter[14];
            parameters[0] = new ReportParameter("rpHoTen", hocVien.HoDem + " " + hocVien.Ten);
            parameters[1] = new ReportParameter("rpGioiTinh", hocVien._GioiTinhStr);
            parameters[2] = new ReportParameter("rpNgaySinh", hocVien.NgaySinh);
            parameters[3] = new ReportParameter("rpNoiSinh", hocVien.NoiSinh);
            parameters[4] = new ReportParameter("rpKhoaNam", khoaNam.ToString());
            parameters[5] = new ReportParameter("rpChuyenNganh", chuyenNganh.TenNganhDaoTao);
            parameters[6] = new ReportParameter("rpMaSoChuyenNganh", chuyenNganh.MaNganhDaoTao);
            parameters[7] = new ReportParameter("rpNgayLap", string.Format("Thừa Thiên Huế, ngày {0} tháng {1} năm {2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year));
            parameters[8] = new ReportParameter("rpChungChi", string.Format("Ngoại ngữ: Chứng chỉ {0} tiếng {1}, {2} cấp ngày {3:dd/MM/yyyy}", hocVien.LoaiChungChi, hocVien.NgoaiNgu, hocVien.NoiCapChungChi, hocVien.NgayCapChungChi));
            parameters[9] = new ReportParameter("rpTenDeTai", hocVien.TenLuanVan);
            parameters[10] = new ReportParameter("rpDiemBaoVe", hocVien.DiemLuanVan.HasValue ? hocVien.DiemLuanVan.Value.ToString() : "");
            parameters[11] = new ReportParameter("rpNgayBaoVe", string.Format("*Bảo vệ tốt nghiệp ngày {0:dd/MM/yyyy} tại Hội đồng chấm luận văn gồm:", hocVien.NgayBaoVe.HasValue ? hocVien.NgayBaoVe.Value : DateTime.MinValue));
            parameters[12] = new ReportParameter("rpHoiDong", hocVien.HoiDongBaoVe);
            parameters[13] = new ReportParameter("rpDTBChung", string.Format("{0:0.00}", hocVien._DiemTichLuy));
            System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
            pg.Margins.Top = 30;
            pg.Margins.Bottom = 20;
            pg.Margins.Left = 20;
            pg.Margins.Right = 20;
            pg.Landscape = false;
            PaperSize size = new PaperSize();
            size.Width = 827;
            size.Height = 1169;
            pg.PaperSize = size;
            this.rvInBangDiemFinal.SetPageSettings(pg);
            this.rvInBangDiemFinal.LocalReport.SetParameters(parameters);

            this.rvInBangDiemFinal.RefreshReport();
            rvInBangDiemFinal.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            rvInBangDiemFinal.PrinterSettings.PrinterName = PrintUtil.GetDefaultPrinter();
            if (!preview)
            {
                //string fileName = string.Format(filePathTemp, hocVien.MaHocVien.Replace(".", ""));
                LocalReport report = this.rvInBangDiemFinal.LocalReport;
                PrintUtil.DoPrint(report);
                //PrintUtil.Export(report, PrintUtil.WORDFORMAT, fileName);
                //Thread.Sleep(timeToWaitExport);
                //if (word == null)
                //    word = new WordUtil();
                //word.PrintDocument(fileName);
            }
            else this.ShowDialog();
        }
    }
}
